###############
##Python Util##
###############
global !p
def math():
    return vim.eval('vimtex#syntax#in_mathzone()') == '1'
def comment(): 
    return vim.eval('vimtex#syntax#in_comment()') == '1'
def env(name):
    [x,y] = vim.eval("vimtex#env#is_inside('" + name + "')") 
    return x != '0' and y != '0'
endglobal


#############
### Math ####
#############
snippet mk "Math" wA
$${1}$`!p
if t[2] and t[2][0] not in [',', '.', '?', '-', ' ']:
	snip.rv = ' '
else:
	snip.rv = ''
`$2
endsnippet

snippet dm "Math" wA
$$
${1:${VISUAL}}
$$ $0
endsnippet

context "math()"
snippet => "implies" Ai
\implies
endsnippet

context "math()"
snippet =< "implied by" Ai
\impliedby
endsnippet

context "math()"
snippet iff "iff" Ai
\iff
endsnippet

context "math()"
snippet ali "Align" bA
\begin{aligned}
	${1:${VISUAL}}
\end{aligned}
endsnippet

context "math()"
snippet // "Fraction" iA	
\\frac{$1}{$2}$0
endsnippet

context "math()"
snippet / "Fraction" iA
\\frac{${VISUAL}}{$1}$0
endsnippet

context "math()"
priority 1000
snippet '((\d+)|(\d*)(\\)?([A-Za-z]+)((\^|_)(\{\d+\}|\d))*)/' "Fraction" wrA
\\frac{`!p snip.rv = match.group(1)`}{$1}$0
endsnippet

context "math()"
snippet '^.*\)/' "() frac" wrA
`!p
stripped = match.string[:-1]
depth = 0
i = len(stripped) - 1
while True:
	if stripped[i] == ')': depth += 1
	if stripped[i] == '(': depth -= 1
	if depth == 0: break;
	i-=1
snip.rv = stripped[0:i] + "\\frac{" + stripped[i+1:-1] + "}"
`{$1}$0
endsnippet

context "math()"
snippet '([A-Za-z])(\d)' "auto subscript" wrA
`!p snip.rv = match.group(1)`_`!p snip.rv = match.group(2)`
endsnippet

context "math()"
snippet '([A-Za-z])_(\d\d)' "auto subscript2" wrA
`!p snip.rv = match.group(1)`_{`!p snip.rv = match.group(2)`}
endsnippet

context "math()"
snippet == "equals"
&= $1 \\\\
endsnippet

context "math()"
snippet sr "^2" 
^2
endsnippet

context "math()"
snippet cb "^3" 
^3
endsnippet

context "math()"
snippet td "to the ... power"  Ai
^{$1}$0
endsnippet

context "math()"
snippet rd "to the ... power" 
^{$1}$0
endsnippet

context "math()"
snippet __ "subscript" Ai
_{$1}$0
endsnippet

priority 100
context "math()"
snippet ^^ "upper" Ai
^{$1}$0
endsnippet

context "math()"
snippet ooo "\infty" 
\infty
endsnippet

context "math()"
snippet rij "mrij" i
(${1:x}_${2:n})_{${3:$2}\\in${4:\\N}}$0
endsnippet

context "math()"
snippet <= "leq" 
\le 
endsnippet

context "math()"
snippet >= "geq" 
\ge 
endsnippet

context "math()"
snippet EE "geq" 
\exists 
endsnippet

context "math()"
snippet AA "forall" 
\forall 
endsnippet

context "math()"
snippet xnn "xn" 
x_{n}
endsnippet

context "math()"
snippet ynn "yn" 
y_{n}
endsnippet


context "math()"
snippet xii "xi" 
x_{i}
endsnippet

context "math()"
snippet yii "yi" 
y_{i}
endsnippet

context "math()"
snippet xjj "xj" 
x_{j}
endsnippet

context "math()"
snippet yjj "yj" 
y_{j}
endsnippet

context "math()"
snippet xp1 "x" 
x_{n+1}
endsnippet

context "math()"
snippet xmm "x" 
x_{m}
endsnippet


context "math()"
snippet mcal "mathcal" A
\mathcal{$1}$0
endsnippet

snippet lll "l"
\ell
endsnippet

context "math()"
snippet nabl "nabla" 
\nabla 
endsnippet

context "math()"
snippet xx "cross" 
\times 
endsnippet

context "math()"
snippet norm "norm"
\|$1\|$0
endsnippet

priority 100
context "math()"
snippet '(?<!\\)(cdot|sin|cos|arccot|cot|csc|ln|log|exp|star|perp)' "ln" rwA
\\`!p snip.rv = match.group(1)` 
endsnippet

priority 300
context "math()"
snippet dint "integral" wA
\int_{${1:-\infty}}^{${2:\infty}} ${3:${VISUAL}} $0
endsnippet

priority 200
context "math()"
snippet '(?<!\\)(arcsin|arccos|arctan|arccot|arccsc|arcsec|pi|zeta|int)' "ln" rwA
\\`!p snip.rv = match.group(1)`
endsnippet


priority 100
context "math()"
snippet -> "to" iA
\to 
endsnippet

priority 200
context "math()"
snippet <-> "leftrightarrow" 
\leftrightarrow
endsnippet

context "math()"
snippet !> "mapsto" 
\mapsto 
endsnippet

context "math()"
snippet invs "inverse" 
^{-1}
endsnippet

context "math()"
snippet compl "complement" 
^{c}
endsnippet

context "math()"
snippet \\\ "setminus" 
\setminus
endsnippet

context "math()"
snippet >> ">>" w
\gg
endsnippet

context "math()"
snippet << "<<" w
\ll
endsnippet

context "math()"
snippet set "set" wA
\\{$1\\} $0
endsnippet

context "math()"
snippet || "mid" 
 \mid 
endsnippet

context "math()"
snippet cc "subset" Ai
\subset 
endsnippet

snippet notin "not in " 
\not\in 
endsnippet

context "math()"
snippet inn "in " 
\in 
endsnippet

context "math()"
snippet NN "n" 
\N
endsnippet

context "math()"
snippet Nn "cap" 
\cap 
endsnippet

context "math()"
snippet UU "cup" 
\cup 
endsnippet

context "math()"
snippet uuu "bigcup" 
\bigcup_{${1:i \in ${2: I}}} $0
endsnippet

context "math()"
snippet nnn "bigcap" 
\bigcap_{${1:i \in ${2: I}}} $0
endsnippet

context "math()"
snippet OO "emptyset" 
\O
endsnippet

context "math()"
snippet RR "real" 
\R
endsnippet

context "math()"
snippet QQ "Q" 
\Q
endsnippet

context "math()"
snippet ZZ "Z" 
\Z
endsnippet

context "math()"
snippet <! "normal" 
\trngleleft 
endsnippet

context "math()"
snippet <> "hokje" 
\dmond 
endsnippet


context "math()"
snippet '(?<!i)sts' "text subscript" irA
_\text{$1} $0
endsnippet

context "math()"
snippet tt "text" 
\text{$1}$0
endsnippet

context "math()"
snippet case "cases" wA
\begin{cases}
	$1
\end{cases}
endsnippet

context "math()"
snippet SI "SI" 
\SI{$1}{$2}
endsnippet

snippet cvec "column vector" 
\begin{pmatrix} ${1:x}_${2:1}\\\\ \vdots\\\\ $1_${2:n} \end{pmatrix}
endsnippet

priority 10
context "math()"
snippet "bar" "bar" r
\overline{$1}$0
endsnippet

priority 100
context "math()"
snippet "([a-zA-Z])bar" "bar" r
\overline{`!p snip.rv=match.group(1)`}
endsnippet

priority 10
context "math()"
snippet "hat" "hat" r
\hat{$1}$0
endsnippet

priority 100
context "math()"
snippet "([a-zA-Z])hat" "hat" r
\hat{`!p snip.rv=match.group(1)`}
endsnippet

context "math()"
snippet HH "H" 
\mathbb{H}
endsnippet

context "math()"
snippet DD "D" 
\mathbb{D}
endsnippet

context "math()"
snippet != "unequals" 
\neq 
endsnippet

context "math()"
snippet ceil "ceil" 
\left\lceil $1 \right\rceil $0
endsnippet

context "math()"
snippet floor "floor" 
\left\lfloor $1 \right\rfloor$0
endsnippet

snippet pmat "pmat" 
\begin{pmatrix} $1 \end{pmatrix} $0
endsnippet

context "math()"
snippet bmat "bmat" 
\begin{bmatrix} $1 \end{bmatrix} $0
endsnippet

context "math()"
snippet () "left( right)" 
\left( ${1:${VISUAL}} \right) $0
endsnippet


snippet lra "leftangle rightangle" 
\left<${1:${VISUAL}} \right>$0
endsnippet

context "math()"
snippet conj "conjugate" 
\overline{$1}$0
endsnippet

context "math()"
snippet sum "sum" w
\sum_{n=${1:1}}^{${2:\infty}} ${3:a_n z^n}
endsnippet

context "math()"
snippet taylor "taylor" w
\sum_{${1:k}=${2:0}}^{${3:\infty}} ${4:c_$1} (x-a)^$1 $0
endsnippet

context "math()"
snippet lim "limit" w
\lim_{${1:n} \to ${2:\infty}} 
endsnippet

context "math()"
snippet limsup "limsup" w
\limsup_{${1:n} \to ${2:\infty}} 
endsnippet

context "math()"
snippet prod "product" w
\prod_{${1:n=${2:1}}}^{${3:\infty}} ${4:${VISUAL}} $0
endsnippet

context "math()"
snippet part "d/dx" w
\frac{\partl ${1:V}}{\partl ${2:x}} $0
endsnippet

context "math()"
snippet sq "\sqrt{}" 
\sqrt{${1:${VISUAL}}} $0
endsnippet

context "math()"
snippet ~ "~"
\sim 
endsnippet

context "math()"
snippet ~~ "approx"
\approx
endsnippet

context "math()"
snippet != "nq" 
\neq
endsnippet

context "math()"
snippet +- "pm" wA
\pm
endsnippet

priority 100
context "math()"
snippet '(?<!\\)(alpha|beta|gamma|delta|Delta|epsilon|varepsilon|zeta|eta|theta|Theta|vartheta|mu|nu|xi|pi|rho|sigma|tau|phi|Phi|varphi|omega|Omega|chi)' "greece" rwA
\\`!p snip.rv = match.group(1)`
endsnippet


priority 100
context "math()"
snippet '(?<!\\)(vec)' "latexmath" rwA
\\`!p snip.rv = match.group(1)`{$0}
endsnippet

############
## Others ##
############
snippet date "YYYY-MM-DD" w
`!v strftime("%Y-%m-%d")`
endsnippet

snippet ddate "Month DD, YYYY" w
`!v strftime("%b %d, %Y")`
endsnippet

snippet diso "ISO format datetime" w
`!v strftime("%Y-%m-%d %H:%M:%S%z")`
endsnippet

snippet time "hh:mm" w
`!v strftime("%H:%M")`
endsnippet

snippet datetime "YYYY-MM-DD hh:mm" w
`!v strftime("%Y-%m-%d %H:%M")`
endsnippet

##############
## Markdown ##
##############
snippet title "title"
<head><style>div.solid {border-style:solid;}</style></head>
<b><font size="7">$1</font></b>

------

$0
endsnippet

snippet newpage "New Page"
<div style="page-break-after: always;"></div>
$0
endsnippet

snippet sp
&emsp;
endsnippet

context "nomath()"
snippet { "{}" A
{$1}$0
endsnippet

context "nomath()"
snippet [ "[]" A
[$1]$0
endsnippet

context "nomath()"
snippet ( "()" A
($1)$0
endsnippet

priority 80
context "nomath()"
snippet + "*" Ai
*$1*$0
endsnippet

priority 80
context "nomath()" 
snippet **a "+" i
+
endsnippet

snippet fig "Figure environment" bw
![$1](pic/$1.png)
$0
endsnippet

snippet figt "Figure with title" b
<center>
    <img style="border-radius: 0.2125em;" src="pic/$1.png";size="50">
    <div style="
    display: outline;
    color: #666;
    padding: 2px;">$2 </div>
</center>
$0
endsnippet

snippet table "table" 
|$1|$2|
|:-|:-:|
|$3|$0|
endsnippet

snippet msp "4 space" w
&emsp; &emsp; &emsp; &emsp;
endsnippet

snippet deg "degree" w
&deg;$C$
endsnippet

snippet dia "diamond" w
&diams;
endsnippet

snippet link  "hlink" w
[$1]($2)$0 
endsnippet

snippet club "spade"
$\clubsuit$
endsnippet

snippet lrn "left( right)" iAw
($1)$0
endsnippet

snippet lrb "left\{ right\}" iAw
{$1}$0
endsnippet

snippet lre "left[ right]" iAw
[$1]$0
endsnippet

snippet '(?<!\\)([0-9])([0-9])tb' "definele" r
$1`!p 
x=match.group(1)
y=match.group(2)
row1=""
row2="" 
for i in range(int(x)):
	row1+="| "
	row2+="|:-:"
row1+="|\n"
row2+="|\n"
out=row1+row2+int(y)*row1
snip.rv=out
`$0
endsnippet

snippet box "box" w
<div class="solid">
$1
</div>
$0
endsnippet

# vim:ft=snippets
